package com.ewing.busi.carry.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.ewing.busi.carry.constants.RegionType;
import com.ewing.busi.carry.dao.WebCarryTemplateDao;
import com.ewing.busi.carry.dao.WebCarryWayDao;
import com.ewing.busi.carry.dto.WebCarryWayDto;
import com.ewing.busi.carry.dto.WebCarryWayResp;
import com.ewing.busi.carry.model.WebCarryWay;
import com.ewing.busi.order.dao.OrderDetailDao;
import com.ewing.busi.order.dto.OrderInfoDetailResp;
import com.ewing.busi.resource.dao.WebResourceDao;
import com.ewing.busi.resource.model.WebResource;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 
 * @author chenxuegui@163.com
 * @createDate 2016年9月3日
 */
@Repository("webCarryService")
public class WebCarryService {

  @Resource
  private WebCarryTemplateDao webCarryTemplateDao;
  @Resource
  private WebCarryWayDao webCarryWayDao;
  @Resource
  private WebResourceDao webResourceDao;
  @Resource
  private OrderDetailDao orderDetailDao;

  /**
   * 
   * @param shopId
   * @param region
   * @author Joeson
   */
//  public List<WebCarryWayDto> findByTemplateIdAndRegion(Integer templateId, String region) {
//    if (null == templateId || StringUtils.isEmpty(region)) {
//      return Collections.emptyList();
//    }
//
//    List<WebCarryWay> wayList = webCarryWayDao.findByTemplateIdAndRegion(templateId, region);
//    Map<String, WebCarryWay> carryWay2Way = Maps.newHashMap();
//    for (WebCarryWay way : wayList) {
//      if (carryWay2Way.containsKey(way.getCarryWay())
//          || ObjectUtils.equals(way.getRegionType(), RegionType.GLOBAL.value)) {
//        continue;
//      }
//      carryWay2Way.put(way.getCarryWay(), way);
//    }
//
//    return WebCarryWayDto.to((List<WebCarryWay>) carryWay2Way.values());
//  }

  /**
   * 查找运费模板
   * 
   * @param resIds
   * @param region
   * @author Joeson
   */
  public List<WebCarryWayDto> findByResIdsAndRegion(List<Integer> resIds, String region) {
    if (CollectionUtils.isEmpty(resIds) || StringUtils.isEmpty(region)) {
      return Collections.emptyList();
    }

    return findByResourceAndRegion(webResourceDao.findByIds(resIds), region);
  }
  
  /**
   * 查找运费模板
   * 
   * @param resIds
   * @param region
   * @author Joeson
   */
  public List<WebCarryWayDto> findByResourceAndRegion(List<WebResource> resourceList, String region) {
    if (CollectionUtils.isEmpty(resourceList) || StringUtils.isEmpty(region)) {
      return Collections.emptyList();
    }

    // 运费id
    Map<Integer, List<Integer>> tempId2resIdList = Maps.newHashMap();
    for (WebResource resource : resourceList) {
      List<Integer> resIdList = tempId2resIdList.get(resource.getCarryTemplateId());
      if(resIdList == null){
        resIdList = Lists.newArrayList();
        tempId2resIdList.put(resource.getCarryTemplateId(), resIdList);
      }
      resIdList.add(resource.getId());
    }

    List<WebCarryWay> wayList = webCarryWayDao
        .findByTemplateIdsAndRegion(new ArrayList<Integer>(tempId2resIdList.keySet()), region);
    Map<String, WebCarryWay> carryWay2Way = Maps.newHashMap();
    for (WebCarryWay way : wayList) {
      if (carryWay2Way.containsKey(way.getCarryWay())
          || ObjectUtils.equals(way.getRegionType(), RegionType.GLOBAL.value)) {
        continue;
      }

      carryWay2Way.put(way.getCarryWay(), way);
    }

    return WebCarryWayDto.to(new ArrayList<WebCarryWay>(carryWay2Way.values()), tempId2resIdList);
  }

  public List<WebCarryWayResp> findByOrderIdAndRegion(Integer cusId, Integer orderId,
      String city) {
    if (null == cusId || null == orderId || StringUtils.isEmpty(city)) {
      return Collections.emptyList();
    }

    Map<Integer, Integer> resId2Count = Maps.newHashMap();
    List<OrderInfoDetailResp> list = orderDetailDao.findByOrderIdAndCusId(orderId, cusId);
    for (OrderInfoDetailResp resp : list) {
      resId2Count.put(resp.getResourceId(), resp.getItemCount());
    }
    
    Map<String, List<WebCarryWayDto>> carryWay2WayList = Maps.newHashMap();
    List<WebCarryWayDto> dtoList = findByResIdsAndRegion(new ArrayList<Integer>(resId2Count.keySet()), city);
    for (WebCarryWayDto dto : dtoList) {
      List<WebCarryWayDto> wayList = carryWay2WayList.get(dto.getCarryWay());
      if (null == wayList) {
        wayList = Lists.newArrayList();
        carryWay2WayList.put(dto.getCarryWay(), wayList);
      }

      wayList.add(dto);
    }

    return WebCarryWayResp.to(carryWay2WayList, resId2Count);
  }

}
